package taskit.datastructures;

import taskit.logic.ParserReturnObject;
import taskit.storage.Data;
import taskit.test.Log;

//@author A0087072A
public class CommandUpdate extends Command {
	private Task task2;
	private Data data;
	public CommandUpdate(Task task1, Task task2, Data data) {
		this.task = task1;
		this.task2 = task2;
		this.data = data;
	}
	@Override
	protected void execute(boolean test) throws Exception {
		Log.add("CommandUpdate.java: execute()");
		if ((task instanceof Floating) && (task2 instanceof Floating)) {
			data.delete((Floating) task);
			data.add((Floating) task2);
			ParserReturnObject.setAction(ParserReturnObject.ACTION_UPDATE_FT);
			if(test) {
				testResult = CMD_ADD_TODO;
			}
		} else if ((task instanceof Schedule) && (task2 instanceof Schedule)) {
			data.delete((Schedule) task);
			data.add((Schedule) task2);
			ParserReturnObject.setAction(ParserReturnObject.ACTION_UPDATE_DAS);
			if(test) {
				testResult = CMD_ADD_EVENT;
			}
		} else if ((task instanceof Deadline) && (task2 instanceof Deadline)) {
			data.delete((Deadline) task);
			data.add((Deadline) task2);
			ParserReturnObject.setAction(ParserReturnObject.ACTION_UPDATE_DAS);
			if(test) {
				testResult = CMD_ADD_DEADLINE;
			}
		} else {
			throw new Exception("Cannot update this task");
		}

	}
	
	@Override
	public Command reverse() {
		return new CommandUpdate(task2, task, data);
	}
}
